package com.axin.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.axin.blog.api.domain.Blog;
import com.axin.blog.api.domain.BlogCategory;
import com.axin.blog.api.dto.BlogDTO;
import com.axin.blog.api.vo.BlogMonthArchive;
import com.axin.blog.api.vo.BlogVo;
import com.axin.blog.mapper.BlogMapper;
import com.axin.blog.service.IBlogCategoryService;
import com.axin.blog.service.IBlogService;
import com.axin.blog.service.IBlogTagService;
import com.axin.common.core.constant.Constants;
import com.axin.common.core.constant.SecurityConstants;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.mybatis.utils.PageUtils;
import com.axin.common.security.model.LoginUser;
import com.axin.common.security.utils.SecurityUtils;
import com.axin.system.api.domain.SysDictData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 博客管理 业务层实现
 *
 * @Author axin
 * @Date 2021/10/29 21:00
 **/
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {

    @Autowired
    private IBlogCategoryService categoryService;

    @Autowired
    private IBlogTagService tagService;

    /**
     * 根据参数分页查询博客列表
     *
     * @param blog
     * @return
     */
    @Override
    public TableDataInfo selectPageBlogList(Blog blog) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (!loginUser.getUser().isAdmin()) {
            blog.setUserId(loginUser.getUserId());
        }
        return PageUtils.buildDataInfo(baseMapper.selectPageBlogList(PageUtils.buildPage(), blog));
    }


    /**
     * 新增博客
     *
     * @param blogDTO
     * @return 结果
     */
    @Override
    public int insertBlog(BlogDTO blogDTO) {

        Blog blog = new Blog();
        BeanUtil.copyProperties(blogDTO, blog);
        // 标签处理
        if (ArrayUtil.isNotEmpty(blogDTO.getTagIds())) {
            blog.setTagIds(StrUtil.join(",", blogDTO.getTagIds()));
        } else{
            blog.setTagIds("");
        }
        blog.setStatus(1);
        return baseMapper.insert(blog);
    }

    /**
     * 查询博客信息
     *
     * @param blogId
     * @return
     */
    @Override
    public BlogVo selectBlogById(Long blogId) {
        BlogVo vo = new BlogVo();
        Blog blog = baseMapper.selectById(blogId);
        BeanUtil.copyProperties(blog, vo);
        // 分类回显
        vo.setCategoryName(categoryService.getById(blog.getCategoryId()).getCategoryName());
        // 标签回显
        if (ArrayUtil.isNotEmpty(vo.getTagIds())) {
            vo.setTags(tagService.listByIds(Arrays.asList(vo.getTagIds())));
        }
        return vo;
    }

    /**
     * 修改博客信息
     *
     * @param blogDTO 博客信息
     * @return 结果
     */
    @Override
    public int updateBlog(BlogDTO blogDTO) {
        Blog blog = new Blog();

        BeanUtil.copyProperties(blogDTO, blog);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser.getUser().isAdmin()) {
            blog.setUserId(blog.getUserId());
        } else {
            blog.setUserId(loginUser.getUserId());
        }

        // 标签处理
        if (ArrayUtil.isNotEmpty(blogDTO.getTagIds())) {
            blog.setTagIds(StrUtil.join(",", blogDTO.getTagIds()));
        } else{
            blog.setTagIds("");
        }
        return baseMapper.updateById(blog);
    }





    /**
     * 批量删除分类
     *
     * @param blogIds 需要删除的博客ID
     * @return 结果
     */
    @Override
    public void deleteBlogByIds(Long[] blogIds) {
        baseMapper.deleteBatchIds(Arrays.asList(blogIds));
    }


    /*****************************************前台  ****************************************************************/

    /**
     * 分页查询博客列表
     *
     * @param blog
     * @return
     */
    @Override
    public TableDataInfo getNewBlogPageList(Blog blog) {

        Page<Blog> pageList = baseMapper.getNewBlogPageList(PageUtils.buildPage(), blog);

        // 回显数据处理
        List<Blog> records = pageList.getRecords();

        for (Blog item : records) {
            if (StrUtil.isNotEmpty(item.getTagIds())) {
                item.setTags(tagService.listByIds(StrUtil.split(item.getTagIds(), ",")));
            }
        }
        pageList.setRecords(records);



        return PageUtils.buildDataInfo(pageList);
    }

    /**
     * 获取时间日期归档
     *
     * @return
     */
    @Override
    public List<BlogMonthArchive> getMonthArchiveList() {
        List<BlogMonthArchive> monthArchive = new ArrayList<>();

        return baseMapper.getMonthArchiveList();

    }
}
